package com.aco.fatureapp.service;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.aco.fatureapp.auxiliary.FaturaRetidaItem;
import com.orm.SugarApp;
import com.orm.SugarDb;

public class SrvReports {

	private SQLiteDatabase mDatabase = null;

	public SrvReports() {
		super();

		this.mDatabase = new SugarDb(SugarApp.getSugarContext())
				.getWritableDatabase();
	}

	/**
	 *
	 * @param inscricao
	 * @return
	 */
	public List<FaturaRetidaItem> getLstFaturasRetidas(Long inscricao) {
		final String[] param = { inscricao.toString() };

		final Cursor cursor = mDatabase.rawQuery(""
				+ " SELECT inscricao, conta_retida, cod_ocorrencia1 "
				+ " FROM log_leitura "
				+ " WHERE conta_retida > 0 AND inscricao = ? ", param);

		List<FaturaRetidaItem> result = null;
		cursor.moveToFirst();

		if (cursor.getCount() > 0) {
			result = new ArrayList<FaturaRetidaItem>();

			do {
				FaturaRetidaItem item = new FaturaRetidaItem();
				item.setInscricao(cursor.getLong(0));
				item.setTipoRetencao(cursor.getInt(1));
				item.setCodOcorrencia(cursor.getLong(2));

				result.add(item);
			}
			while (cursor.moveToNext());

			cursor.close();
		}

		return result;
	}

	/**
	 *
	 * @return
	 */
	public List<FaturaRetidaItem> getLstFaturasRetidas() {
		final Cursor cursor = mDatabase.rawQuery(""
				+ " SELECT inscricao, conta_retida, cod_ocorrencia1 "
				+ " FROM log_leitura WHERE conta_retida > 0 ", null);

		List<FaturaRetidaItem> result = null;
		cursor.moveToFirst();

		if (cursor.getCount() > 0) {
			result = new ArrayList<FaturaRetidaItem>();

			do {

				FaturaRetidaItem item = new FaturaRetidaItem();
				item.setInscricao(cursor.getLong(0));
				item.setTipoRetencao(cursor.getInt(1));
				item.setCodOcorrencia(cursor.getLong(2));

				result.add(item);
			}
			while (cursor.moveToNext());

			cursor.close();
		}

		return result;
	}

}
